// 封装分类相关的业务代码
import {ref, onMounted} from 'vue'
import {useRoute, onBeforeRouteUpdate} from 'vue-router'
import {getCategoryAPI} from '@/apis/category'

// 分类逻辑;
export function useCategory() {
  const categoryData = ref({})
  const route = useRoute();
  const getCategory = async(id = route.params.id)=>{
    const res = await getCategoryAPI(id)
    categoryData.value = res.result;
  }

  // watch(
  //       () => route.params.id,
  //       (newId, oldId) => getCategory()
  //     , {immediate:true});

  // 路由变化后，再进行重新获取商品列表.
  onBeforeRouteUpdate((to)=>{
    getCategory(to.params.id)
  })

  onMounted(()=>{
    getCategory()
  })

  return {categoryData}
}
